package main

import (
	"fmt"
)

func printSlice(s []int) {
	fmt.Printf("%v, len=%d, cap=%d\n", s, len(s), cap(s))
}

func sliceOps() {
	fmt.Println("--- Creating Slice ---")
	var s []int
	fmt.Println(s == nil) // Zero value for slice is nil
	for i := 0; i < 5; i++ {
		printSlice(s)
		s = append(s, 2*i+1)
	}
	fmt.Println("s =", s)

	// 可以在创建时指定len和cap
	s2 := make([]int, 16)
	s3 := make([]int, 10, 32)
	printSlice(s2)
	printSlice(s3)

	fmt.Println("--- Copying slice ---")
	copy(s2, s)
	printSlice(s2)

	fmt.Println("--- Deleting elements from slice ---")
	s2 = append(s2[:3], s2[4:]...) // sames as delete elements of 4th
	printSlice(s2)

	fmt.Println("--- Popping one elements form front ---")
	front := s2[0]
	s2 = s2[1:]
	fmt.Println("front elements is :", front)
	printSlice(s2)

	fmt.Println("--- Popping one elements from back ---")
	tail := s2[len(s2)-1]
	s2 = s2[:len(s2)-1]
	fmt.Println("tail elements is :", tail)
	printSlice(s2)

}

func main() {

	sliceOps()
}
